//
//  Position.swift
//  SpuerYangAngWang
//
//  Created by 杨茂盛 on 2016/12/22.
//  Copyright © 2016年 杨茂盛. All rights reserved.
//

import Foundation
extension UIButton{
    func setImagePosition(spacing:CGFloat) {
        
        let imageWith = self.imageView?.image?.size.width
        let imageHeight = self.imageView?.image?.size.height
        let labelWidth = self.titleLabel?.attributedText?.size().width
        let labelHeight = self.titleLabel?.attributedText?.size().height
        
        if labelWidth == nil && labelHeight == nil {
            return
        }
        if imageWith == nil && imageHeight == nil {
            return
        }
        //image中心移动的x距离
        let imageOffsetX = labelWidth! / 2
        //image中心移动的y距离
        let imageOffsetY = labelHeight! / 2 + spacing / 2
        //label中心移动的x距离
        let labelOffsetX = imageWith!/2
        //label中心移动的y距离
        let labelOffsetY = imageHeight! / 2 + spacing / 2
        
        self.imageEdgeInsets = UIEdgeInsetsMake(-imageOffsetY, imageOffsetX, imageOffsetY, -imageOffsetX);
        self.titleEdgeInsets = UIEdgeInsetsMake(labelOffsetY, -labelOffsetX, -labelOffsetY, labelOffsetX);
    }
}
